#include<stdio.h>
#include<string.h>
#include<stdlib.h>

// 定义二叉树节点结构体
struct treenode {
    int data;
    struct treenode* left;
    struct treenode* right;
};

// 定义存储中根和后根序列的结构体
struct value {
    char post[30];
    char in[30];
};

// 定义二叉树数组
struct treenode* troot[10];

// 先根遍历
void preorder(struct treenode* root) {
    if (root == NULL) return;
    printf("%d ", root->data);
    preorder(root->left);
    preorder(root->right);
}

// 中根遍历
void inorder(struct treenode* root) {
    if (root == NULL) return;
    inorder(root->left);
    printf("%d ", root->data);
    inorder(root->right);
}

// 后根遍历
void postorder(struct treenode* root) {
    if (root == NULL) return;
    postorder(root->left);
    postorder(root->right);
    printf("%d ", root->data);
}

// 计算二叉树的深度
int depth(struct treenode* root) {
    if (root == NULL) return -1;
    int d1 = depth(root->left);
    int d2 = depth(root->right);
    return (d1 > d2)? d1 + 1 : d2 + 1;
}

// 在中根序列中查找根节点的位置
int findroot(char* inorder, int size, char val) {
    for (int i = 0; i < size; i++)
        if (inorder[i] == val) return i;
    return -1;
}

// 根据后根和中根序列构建二叉树
struct treenode* build(char*postorder,char* inorder,int n) {
    if (n <= 0) return NULL;
    struct treenode* root = (struct treenode*)malloc(sizeof(struct treenode));
    root->data = postorder[n];
    int k = findroot(inorder, n, root->data);
    root->left = build(&postorder[0], &inorder[0], k - 1);
    root->right = build(&postorder[k+1], &inorder[k+1], n-k - 1);
    return root;
}

int main() {
    struct value val[15];
    int i = 0;
    while (scanf("%s%s", val[i].post, val[i].in) == 2) {
        int n = (int)sizeof(val[i].post) / sizeof(char) - 1;
        troot[i] = build(val[i].post, val[i].in, n);
        i++;
        if (i >= 10) break; // 限制最多构建10棵二叉树
    }
    for (int m = 0; m < i; m++) {
        printf("%d\n", depth(troot[m]));
        preorder(troot[m]);
        printf("\n");
    }
    return 0;
}
